<template>
	<view>
		<view>
			<uni-data-select v-model="addParams.provinceId" :localdata="provinceList" @change="onChangeProvince"
				:clear="false"></uni-data-select>
		</view>
		<view>
			<uni-easyinput class="uni-mt-5" trim="all" v-model="addParams.city" placeholder="城市"></uni-easyinput>
		</view>
		<view>
			<uni-easyinput class="uni-mt-5" trim="all" v-model="addParams.county" placeholder="县/区"></uni-easyinput>
		</view>
		<view class="">
			<uni-easyinput type="textarea" v-model="addParams.detail" placeholder="详细地址"></uni-easyinput>
		</view>
		<view>
			<uni-easyinput class="uni-mt-5" trim="all" v-model="addParams.addresstel" placeholder="手机号"></uni-easyinput>
		</view>
		<view>
			<uni-easyinput class="uni-mt-5" trim="all" v-model="addParams.addressName"
				placeholder="收货人"></uni-easyinput>
		</view>
		<view>
			<button class="mini-btn" type="primary" size="mini" @click="add">确认添加</button>
		</view>
	</view>
</template>

<script>
	export default {
		onLoad() {
			let jwt = uni.getStorageSync('authority');
			uni.request({
				url: "api/appuser/selProvince",
				method: 'GET',
				header: {
					"authority": jwt
				},
				success: (res) => {
					console.log(res);
					if (res.data.code == 200) {
						this.provinceList = res.data.data.map(item => ({
							text: item.province,
							value: item.id
						}));
					}
				}
			})
		},
		data() {
			return {
				provinceList: [],
				addParams: {
					provinceId: "",
					city: "",
					county: "",
					detail: "",
					addresstel: "",
					addressName: ""
				}
			};
		},
		methods: {
			add() {
				if (this.addParams.provinceId == "" || this.addParams.city == "" || this.addParams.county ==
					"" || this.addParams.detail == "") {
					uni.showToast({
						title: "请将地址填写完整",
						icon: 'error'
					})
					return;
				}
				let jwt = uni.getStorageSync('authority');
				uni.request({
					url: "api/appuser/addUserAddress",
					method: "POST",
					header: {
						"authority": jwt
					},
					data: this.addParams,
					success: (res) => {
						// console.log(res);
						if (res.data.code == 200) {
							uni.showToast({
								title: "添加成功",
								icon: 'success'
							})
						}
						setTimeout(function() {
							uni.reLaunch({
								url: "/pages/list/list",
							})
						}, 1500);
					}
				})
			},
			onChangeProvince(val) {
				// console.log(val);
				this.addParams.provinceId = val;
			}

		}
	}
</script>

<style lang="scss">

</style>